package com.darkevil.jdspider

import com.darkevil.jdspider.entity.Category
import com.darkevil.jdspider.mapper.CategoryMapper
import com.darkevil.jdspider.service.ProductService
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.core.task.AsyncTaskExecutor
import org.springframework.core.task.TaskExecutor
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import org.springframework.test.context.junit4.SpringRunner
import java.util.*

@RunWith(SpringRunner::class)
@SpringBootTest
class JdSpiderApplicationTests {

    @Autowired
    private lateinit var categoryMapper: CategoryMapper
    @Autowired
    private lateinit var productService: ProductService

    @Autowired
    private lateinit var taskExecutor: AsyncTaskExecutor

    @Test
    fun contextLoads() {

    }

//    @Test
//    fun testAsync() {
//        while (true){
//            for (i in 1..10) {
//                productService.testAsync()
//            }
////        println((taskExecutor as ThreadPoolTaskExecutor).setWaitForTasksToCompleteOnShutdown(true))
////        (taskExecutor as ThreadPoolTaskExecutor).shutdown()
//            while (true) {
//                println("task completed "+(taskExecutor as ThreadPoolTaskExecutor).threadPoolExecutor.completedTaskCount)
//                println("task count "+ (taskExecutor as ThreadPoolTaskExecutor).threadPoolExecutor.taskCount)
//                Thread.sleep(1000)
//                if((taskExecutor as ThreadPoolTaskExecutor).threadPoolExecutor.completedTaskCount==(taskExecutor as ThreadPoolTaskExecutor).threadPoolExecutor.taskCount){
//
//                    break
//                }
//            }
//            println("执行完成")
//            Thread.sleep(2000)
//        }
//    }

    @Test
    fun testBatch() {
        val list = arrayListOf<Category>()
        for (x in 1..10) {
            list.clear()
            println("生成数据")
            for (i in 1..5000) {
                list.add(Category(id = UUID.randomUUID().toString().replace("-", ""), name = "test", url = "test"))
            }
            println("生成数据结束")
            var start = System.currentTimeMillis()
            categoryMapper.batchReplace(list)
            println("方法一 耗时:${System.currentTimeMillis() - start} ms")
        }
//        list.clear()
//        println("生成数据1")
//        for (i in 1..1000) {
//            list.add(Category(id = UUID.randomUUID().toString().replace("-",""), name = "test", url = "test"))
//        }
//        println("生成数据结束1")
//        start=System.currentTimeMillis()
//        list.forEach { categoryMapper.replace(it) }
//        println("方法二 耗时:${System.currentTimeMillis()-start} ms")
    }

}
